package 二叉树;

public class isBalanced {
    public boolean isBalanced(TreeNode root) {
        if(root==null){
            return true;
        } else {
            return Math.abs(dfs(root.left) - dfs(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right);
        }
    }
    public int dfs(TreeNode root){
        if(root==null){
            return 0;
        }
        int left=dfs(root.left);
        int right=dfs(root.right);

        return Math.max(left,right)+1;
    }
}
